package com.qf.web;


import com.qf.page.PageBean;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/findAll")
public class FindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");

        //分页
        String cp = req.getParameter("currentPage");
        String ps = req.getParameter("pageSize");
        //首页

        int currentPage=0;
        if(cp==null||"".equals(cp)){
            currentPage=1;//默认是首页
        }else{
            //当前页
            currentPage=Integer.parseInt(cp);
        }
        int pageSize=0;
        if (ps==null||"".equals(ps)){
            //默认每页显示5条
            pageSize=10;
        }else {
            pageSize=Integer.parseInt(ps);
        }

        //创建分页对象
        PageBean<User> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);


        //调用业务层
        UserService userService=new UserServiceImpl();
        if (username!=null&&!"".equals(username)){
            //调用通过模糊查询分页的业务层
            pageBean = userService.findAll(pageBean ,username);

        }else {
            //调用分页查询的业务层
            pageBean = userService.findAll(pageBean);

        }
        req.setAttribute("users",pageBean);
        req.getRequestDispatcher("/list.jsp").forward(req,resp);




//        List<User> all = userService.findAll();
//        req.setAttribute("users",all);
//
//
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
